#include "stdafx.h"
#include "QGraphicsItemQuestion.h"
#include "memorygame.h"

QBrush QGraphicsItemQuestion::m_backroundBrushNotAnswered( QColor( 100, 200, 125 ) );
QBrush QGraphicsItemQuestion::m_backroundBrushBadAnswered( QColor( 255, 10, 10 ) );
QBrush QGraphicsItemQuestion::m_backroundBrushOkAnswered( QColor( 125, 200, 100 ) );
QFont QGraphicsItemQuestion::m_font( "Times", 20, QFont::Bold );

QGraphicsItemQuestion::QGraphicsItemQuestion( int x, int y, int width, int height, MemoryGame & parent, const Question & question )
:   QGraphicsRectItem( x, y, width, height ),
    m_parent( parent ),
    m_question( question )
{
   // setFlag( QGraphicsItem::ItemIsMovable );
   // setFlag( QGraphicsItem::ItemIsSelectable );
}

QGraphicsItemQuestion::~QGraphicsItemQuestion()
{
}

void QGraphicsItemQuestion::paint( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget )
{
    QString itemStr;
    QRectF r( rect() );
    switch (m_question.answered)
    {
    case QuestionStatus::NotAnswered:
        itemStr = QString( "%1" ).arg( m_question.value );
        painter->setBrush( m_backroundBrushNotAnswered );
        break;

    case QuestionStatus::BadAnswered:
        itemStr = QObject::tr( "Not answered" );
        painter->setBrush( m_backroundBrushBadAnswered );
        break;

    case QuestionStatus::OkAnswered:
        itemStr = QObject::tr( "Answered" );
        painter->setBrush( m_backroundBrushOkAnswered );
        break;
    }

    painter->drawRect( r );
    painter->setFont( m_font );
    QFontMetrics fontMetrics( m_font );

    int offsetX = ( r.width() / 2 ) - ( fontMetrics.width( itemStr ) / 2 );
    int offsetY = ( r.height() / 2 ) + ( fontMetrics.height() / 2 );

    painter->drawText( r.x() + offsetX, r.y() + offsetY, itemStr );
}

void QGraphicsItemQuestion::mouseDoubleClickEvent( QGraphicsSceneMouseEvent * event )
{
    if (m_question.answered == QuestionStatus::NotAnswered)
        m_parent.openQuestion( m_question );
}

qint64 QGraphicsItemQuestion::questionId() const
{
    return m_question.id;
}

void QGraphicsItemQuestion::setQuestionStatus( QuestionStatus::Enum questionStatus )
{
    m_question.answered = questionStatus;
    update();
}
